#include "definition.h"
#include "../MagicDocApp.h"
#include "MagicDocDlgOptions.h"
#include <wx/button.h>
#include <wx/sizer.h>
#include <wx/stattext.h>
#include <wx/statbox.h>
#include <wx/dirdlg.h>
#include <wx/fileconf.h> 
#include "../const_string.h"

enum
{
	ID_OPTION_EXPLORE,
};

BEGIN_EVENT_TABLE(MagicDocDlgOptions, wxDialog)
    // begin wxGlade: MagicDocFrame::event_table
    EVT_BUTTON(ID_OPTION_EXPLORE, MagicDocDlgOptions::OnExplore)
    // end wxGlade
END_EVENT_TABLE();

MagicDocDlgOptions::MagicDocDlgOptions(wxWindow *parent, wxWindowID id,
	const wxString &title, const wxPoint &pos, const wxSize &size, 
	long style,	const wxString &name):
wxDialog(parent, id, title, pos, size, style, name)
{
	wxPanel* panel_generic;
	wxStdDialogButtonSizer *dlg_sizer;
	wxButton *btn_ok, *btn_cancel, *btn_apply;
	wxButton *btn_depotPath;
	wxStaticBoxSizer *static_sizer;
	wxStaticText *st_depotPath;
	
	wxBoxSizer *sizer_main;
	wxBoxSizer *sizer_depotPath;


	btn_ok = new wxButton(this, wxID_OK);
	btn_cancel = new wxButton(this, wxID_CANCEL);
	btn_apply = new wxButton(this, wxID_APPLY);

	panel_generic = new wxPanel( this, -1, wxDefaultPosition, wxSize(200, 200) );

	st_depotPath = new wxStaticText( panel_generic, -1, wxT("Depot path:  ") );
	tc_depotPath = new wxTextCtrl( panel_generic, -1, EMPTY_STRING, wxDefaultPosition,
		wxSize(200, 22) );
	btn_depotPath = new wxButton( panel_generic, ID_OPTION_EXPLORE, wxT("Explore...") );
	cb_copyOnImport = new wxCheckBox( panel_generic, -1, wxT("Copy file while importing document") );
	cb_confirmDelete = new wxCheckBox( panel_generic, -1, wxT("Confirm for deleting document") );

	sizer_depotPath = new wxBoxSizer(wxHORIZONTAL);
	sizer_depotPath->Add(st_depotPath);
	sizer_depotPath->Add(tc_depotPath);
	sizer_depotPath->Add(btn_depotPath);

	static_sizer = new wxStaticBoxSizer( wxVERTICAL, panel_generic, wxT("Generic Settings") );
	static_sizer->Add( sizer_depotPath );
	static_sizer->Add( cb_copyOnImport );
	static_sizer->Add( cb_confirmDelete );
	
	panel_generic->SetSizer(static_sizer);

	dlg_sizer = new wxStdDialogButtonSizer();
	dlg_sizer->AddButton(btn_ok);
	dlg_sizer->AddButton(btn_cancel);
	dlg_sizer->AddButton(btn_apply);
	dlg_sizer->Realize();

	sizer_main = new wxBoxSizer(wxVERTICAL);
	sizer_main->Add(panel_generic, 1, wxEXPAND);
	sizer_main->Add(dlg_sizer);

	SetSizer(sizer_main);
	Layout();
    readConfig();
    UpdateValues();
}

MagicDocDlgOptions::~MagicDocDlgOptions(void)
{
    saveConfig();
}

void MagicDocDlgOptions::OnExplore(wxCommandEvent &event)
{
	wxDirDialog dlg(this, wxT("Select depot storage directory") );
	if( dlg.ShowModal() )
	{
		tc_depotPath->SetLabel( dlg.GetPath() );
        depot_path = dlg.GetPath();
	}
}

void MagicDocDlgOptions::readConfig()
{
    wxFileConfig* config = wxGetApp().GetConfig();
    
    config->Read( CONF_DEPOTPATH, &depot_path );
    config->Read( CONF_COPYONIMPORT, &copy_on_import );
    config->Read( CONF_DELETE_CONFIRM, &confirm_on_delete );
}

void MagicDocDlgOptions::saveConfig()
{
    wxFileConfig* config = wxGetApp().GetConfig();

    config->Write( CONF_DEPOTPATH, depot_path );
    config->Write( CONF_COPYONIMPORT, copy_on_import );
    config->Write( CONF_DELETE_CONFIRM, confirm_on_delete );
}

void MagicDocDlgOptions::UpdateValues()
{
    tc_depotPath->SetValue( depot_path );
    cb_copyOnImport->SetValue( copy_on_import );
    cb_confirmDelete->SetValue( confirm_on_delete );
}